﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace ncp.Web.handler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AdsHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpPostedFile file = context.Request.Files["Filedata"];
            string folder = @context.Request["folder"];
            string uploadPath = HttpContext.Current.Server.MapPath(folder) + "\\";
            if (file != null)
            {
                //格式验证
                string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp",".swf" };          //文件允许格式
                string[] temp = file.FileName.Split('.');
                string currentType = "." + temp[temp.Length - 1].ToLower();
                if (Array.IndexOf(filetype, currentType) != -1)
                {
                    //重命名
                    string fileName = Guid.NewGuid().ToString().Replace("-", "") + currentType;

                    if (!Directory.Exists(uploadPath))
                    {
                        Directory.CreateDirectory(uploadPath);
                    }
                    file.SaveAs(uploadPath + fileName);
                    //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                    context.Response.Write(folder+"/" + fileName);
                }
                else
                {
                    context.Response.Write("0");
                }
            }
            else
            {
                context.Response.Write("0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
